// 新增
router.post('/addstation', async function (req, res, next) {
  let {
    name,
    code,
    department,
    address,
    level,
    x,
    y
  } = req.body;
  await Station.create({
    name,
    code,
    department,
    address,
    level,
    x,
    y
  });
  res.json({
    code: 0,
    msg: "新增成功"
  });
});

// 删除
router.post('/delstation', async function (req, res, next) {
  let {
    id
  } = req.body;
  await Station.destroy({
    where: {
      id
    }
  });
  res.json({
    code: 0,
    msg: "删除成功"
  });
});

// 修改
router.post('/editstation', async function (req, res, next) {
  let {
    id,
    name,
    code,
    department,
    address,
    level,
    x,
    y
  } = req.body;
  await Station.update({
    name,
    code,
    department,
    address,
    level,
    x,
    y
  }, {
    where: {
      id
    }
  });
  res.json({
    code: 0,
    msg: "修改成功"
  });
});

// 根据name模糊查询，分页
router.get('/liststation', async function (req, res, next) {
  let {
    name,
    page,
    limit
  } = req.query;
  let where = {};
  if (name) {
    where.name = {
      [Sequelize.Op.like]: `%${name}%`
    }
  }
  let stations = await Station.findAndCountAll({
    where,
    offset: (page - 1) * limit,
    limit: +limit
  });
  res.json({
    code: 0,
    data: stations
  });
});

// 获取所有
router.get('/allstation', async function (req, res, next) {
  let stations = await Station.findAll();
  res.json(stations);
});





module.exports = router;